#1.
s = "Hello,Python world!"
print(len(s))
#2.
print(s.count('o'))
#3.
print(s.upper())
#4.
print(s.lower())
#5.
print(s.find('Python'))
#6.
s = s.replace('world','universe')
print(s)
#7.
print(s.startswith('Hello'))
#8.
print(s.endswith('!'))
#9.
s = s.split(',')
print(s)
# 10.
print(s.strip('!'))
#11.
print(s[::-1])
#12.
print(s[6:12])
#13.
print(s.replace('Hello,',''))
#14.
s1 = 'Hello,'
s2 = 'World!'
print(s1+s2)
#15.
print(s+str(2021))
#16.
print(s.find('a'))
#17.
print(s.rfind('e'))
#18.
print(s.count(' '))
#19.
for i in range(0,len(s)):
    print(s[i])
#20.
print(list(s))
#21.
char_list = list(s)
char_list.sort()
sorted_str = ''.join(char_list)
print(f" 排序后的字符串:{sorted_str}")
#22.
print(s.isdigit())
#23.
list_new = ['Hello','Python','World!']
s = ' '.join(list_new)
print(s)
#24.
num = 100
formatted_str = "Number: %d" % num
print(f" 格式化结果: {formatted_str}")
#25.
print(s.isupper())
#26.
print(s.islower())
#27.
list3 = [1,2,3]
new = '-'.join(str(list3))
print(new)
#28.
positions = []
for i, char in enumerate(s):
    if char == 'o':
        positions.append(i)
print(f"'o'的位置: {positions}")
#29.
print(s.replace('o','O'))
#30.
insert_pos = 6  # 在索引6的位置插入
new_str = s[:insert_pos] + "amazing " + s[insert_pos:]
print(f" 插入后: {new_str}")

#进阶
s = "Python is great!"
#1.
reversed_str = ""
for i in range(len(s) - 1, -1, -1):
    reversed_str += s[i]
print(reversed_str)  # 输出: "!taerg si nohtyP"
#2.
s = "Python is great!"
print(s.count('t'))
#3.
s = "Python is great!"
print(s.replace('s','_'))
#4.
s2 = "A man, a plan, a canal, Panama!"
# 清理字符串：移除非字母字符并转为小写
cleaned = ''.join(char.lower() for char in s2 if s.isalpha())
print("第7题"+cleaned)
# 检查回文
is_palindrome = cleaned == cleaned[::-1]
print(is_palindrome)  # 输出: True
#5.
s = "Python is great!"
position = s.find("is")
print(position)  # 输出: 7
#6.
s = "Python is great!"
t_index = s.find('t')
if t_index != -1:
    new_str = s[:t_index+1] + "inserted" + s[t_index+1:]
else:
    new_str = s
print(new_str)  # 输出: "Pytinsertedhon is great!"
#7.
s = "Python is great!"
new_str = s.replace("great", "awesome", 1)  # 只替换第一次出现
print(new_str)  # 输出: "Python is awesome!"
#8.
s = "Python is great!"
mid = len(s) // 2
swapped = s[mid:] + s[:mid]
print(swapped)  # 输出: "s great!Python i"
#9.
s = "Python is great!"
words = [word.capitalize() for word in s.split()]
print(words)  # 输出: ['Python', 'Is', 'Great!']
#10.
def encrypt(text):
    # 创建加密映射
    original = "abcdefghijklmnopqrstuvwxyz"
    encrypted = "mnopqrstuvwxyzabcdefghijkl"
    t = str.maketrans(original + original.upper(),encrypted + encrypted.upper())
    return text.translate(t)
s3 = "encryption"
print(encrypt(s3))  # 输出: qzodkbfuaz

